package top.dancun.easbaseinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;
import top.dancun.easbaseinfo.dao.DeptDao;
import top.dancun.easbaseinfo.dao.MajorDao;
import top.dancun.easbaseinfo.entity.DeptEntity;
import top.dancun.easbaseinfo.entity.MajorEntity;
import top.dancun.easbaseinfo.service.MajorService;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/***
 * @author dancun
 */
@Service("majorService")
public class MajorServiceImpl extends ServiceImpl<MajorDao, MajorEntity> implements MajorService {

    @Autowired
    private DeptDao deptDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage page = new Query<MajorEntity>().getPage(params);
        LambdaQueryWrapper<MajorEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Objects.nonNull(params.get("name")) && !"".equals(params.get("name")),
                MajorEntity::getMajorName,
                params.get("name"));
        page = this.baseMapper.list(page, wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<MajorEntity> list(Map<String,String> data) {
        LambdaQueryWrapper<MajorEntity> wrapper = Wrappers.lambdaQuery();
        boolean isNoNull = Objects.nonNull(data.get("deptId"));
        if(isNoNull){
            wrapper.eq(Objects.nonNull(data.get("deptId")),MajorEntity::getDeptId,Integer.parseInt(data.get("deptId")));
        }

        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public MajorEntity getById(Serializable id) {
        MajorEntity major = super.getById(id);
        if(Objects.nonNull(major)){
            DeptEntity dept = deptDao.selectById(major.getDeptId());
            major.setDept(dept);
        }
        return major;
    }
}